package com.wechat.weibo.controller;

import com.wechat.weibo.entity.User;
import com.wechat.weibo.service.LoginService;
import com.wechat.weibo.util.ResultMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Api
@Controller
public class LoginController {
    @Autowired
    LoginService loginService;

//    注册、登录
    @ApiOperation(value = "注册、登录")
    @GetMapping("/login")
    @ResponseBody
    public ResultMap login(String code, String userName,String img){
        if(userName==null||code==null)return ResultMap.wrong();
//        System.out.print(code+"\n");
//        loginService
        User user=loginService.login(code,userName,img);
        if(user==null)return ResultMap.fail(null);
//        System.out.print(user+"\n");
        return ResultMap.success(user);
    }
//    刷新 我的 页面
    @ApiOperation(value = "刷新\"我的\"页面")
    @GetMapping("/refresh")
    @ResponseBody
    public ResultMap freshUser(String openid){
        if(openid==null)return ResultMap.wrong();
        return ResultMap.success(loginService.getUser(openid));
    }

    @RequestMapping("/")
    @ResponseBody
    public String thisIs(){
        return "欢迎访问Cierra的后台";
    }

//    修改信息
    @ApiOperation(value ="修改信息")
    @PostMapping("/reInfo")
    @ResponseBody
    public ResultMap reInfo(String selfID,String username,String brief){
//        System.out.print(selfID+" ! "+username);
        User user=loginService.getUser(selfID);
        if(user==null||username==null)return ResultMap.wrong();
        user.setUserName(username);
        user.setUserBrief(brief);
        return ResultMap.success(loginService.reInfo(user));
    }
    @GetMapping("/searchUser")
    @ResponseBody
    public ResultMap search(String username){
        if(username==null)return ResultMap.wrong();
        return ResultMap.success(loginService.search(username));
    }
}
